<template>
  <div style="height: 100%">
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane label="我的" name="1">
        <div style="align-items: center">
          <span>用户：{{ name }}，你好，欢迎登录！</span>
          <div style="margin-top: 20px">
            <el-card class="box-card">
              <template #header>
                <div class="card-header"><span>修改密码</span></div>
              </template>
              <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="80px" label-position="left">
                <el-form-item label="原密码" prop="password" required size="default" clearable>
                  <el-input v-model="form.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="password1" required size="default" clearable>
                  <el-input v-model="form.password1" type="password"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="password2" required size="default" clearable>
                  <el-input v-model="form.password2" type="password"></el-input>
                </el-form-item>
                <el-footer style="padding-top: 10px">
                  <el-button style="width: 100%" type="primary" size="default" class="btn" :loading="loading"
                             @click="submitForm()">提交
                  </el-button>
                </el-footer>
              </el-form>
            </el-card>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="书架" name="2">
        <el-row :gutter="10">
          <el-col v-for="(item,index) in shelfs" :key="item.id"
                  :xs="24" :sm="12" :md="12" :lg="8" :xl="6"
                  @click="toDetail(item.bookInfo.id)"
          >
            <div class="grid-content ">
              <el-image
                  style="flex:1;width: 100px; height: 100px"
                  :lazy="true"
                  :src="item.bookInfo.bookCover"
                  fit="contain"
              ></el-image>
              <div class="b_title">
                <div style="font-size: 16px;font-weight: 500;padding-bottom: 4px;align-items: center">
                  {{ item.bookInfo.bookName }}
                </div>
                <div><span style="padding-left: 5px;font-size: 14px"> {{ item.bookInfo.bookAuthor }}</span></div>
                <div class="b_desc" style=" ">
                  {{ item.bookInfo.bookIntroduction }}
                </div>
                <div style="padding-top: 5px;display: flex;flex-direction: column">
                  <span style="font-size: 14px">上次读到：{{ item.catalog.catalogName }}</span>
                  <div class="btn-div">
                    <el-button type="success"
                               @click.stop="toContent(item.catalog.id,item.catalog.bid,item.bookInfo.bookName,item.catalog.catalogName)"
                               plain>继续阅读
                    </el-button>
                    <el-button :icon="Delete" @click.stop="delShelf(item.id,index)" circle></el-button>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
        <div v-show="shelfs.length>0 && !last" style="padding:20px;text-align: center">
          <el-alert title="加载更多" type="success" center :closable="false" @click="loadMore"></el-alert>
        </div>
      </el-tab-pane>
      <el-tab-pane label="时间线" name="3">
        <el-timeline>
          <el-timeline-item v-for="item in timeline" :key="item.id" center :timestamp="item.createTime" placement="top">
            <el-card v-if="item.type==='0'">
              <div style="display: flex;flex-direction: row"
                   @click="toDetail(item.bookInfoVo.id)">
                <el-image
                    style="width: 30%; height: 100px;"
                    :lazy="true"
                    :src="item.bookInfoVo.bookCover"
                    fit="contain"
                ></el-image>
                <div class="b_title" style="padding-left: 10px;width: 100%">
                  <div style="font-size: 16px;font-weight: 500;padding-bottom: 4px">
                    {{ item.bookInfoVo.bookName }}
                  </div>
                  <div class="b_desc" style=" ">
                    {{ item.bookInfoVo.bookIntroduction }}
                  </div>
                  <div style="font-size: 16px;padding-top: 5px">
                    {{ item.bookInfoVo.bookAuthor }}
                  </div>
                </div>
              </div>
            </el-card>
            <div v-if="item.type==='1'"
                 @click="toContent(item.catalogVo.id,item.catalogVo.bid,item.bookInfoVo.bookName,item.catalogVo.catalogName)">
              <p>阅读了{{ item.bookInfoVo.bookName }}，{{ item.catalogVo.catalogName }}</p>
            </div>
          </el-timeline-item>
        </el-timeline>
        <div v-show="timeline.length>0 && !last" style="padding:20px;text-align: center">
          <el-alert title="加载更多" type="success" center :closable="false" @click="loadMore"></el-alert>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {Delete,} from '@element-plus/icons-vue'
import {get, del, put} from '../../request/request'
import {repass, timeline, shelfs, delShelf} from "../../request/api";
import {ElLoading, ElMessage} from "element-plus";

export default {
  name: "UCenter",
  data() {
    const reCkPassword = (rule, value, callback) => {
      if (this.form.password1 !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      Delete: Delete,
      activeName: '1',
      info: {},
      uid: '',
      name: '',
      loading: false,
      form: {
        password: '',
        password1: '',
        password2: '',
      },
      rules: {
        password: [
          {required: true, message: '请输入原密码', trigger: 'blur',},
          {max: 20, message: '超过最大长度', trigger: 'blur',},
        ],
        password1: [
          {required: true, message: '请输入新密码', trigger: 'blur',},
          {max: 20, message: '超过最大长度', trigger: 'blur',},
        ],
        password2: [
          {required: true, message: '请输入新密码', trigger: 'blur',},
          {required: true, validator: reCkPassword, trigger: 'blur',},
        ],
      },
      timeline: [],
      shelfs: [],
      pageNum: 1,
      pageSize: 10,
      first: true,
      last: false,
    }
  },
  computed: {
    getInfo() {
      return this.$store.state.user.info
    },
    getName() {
      return this.$store.state.user.name
    },
    getId() {
      return this.$store.state.user.id
    }
  },
  created() {
    this.name = this.getName
    this.info = this.getInfo
    this.uid = this.getId
  },
  methods: {
    handleClick(tab, event) {
      this.activeName = tab.props.name
      if (tab.props.name === '2' || tab.props.name === '3') {
        this.timeline = []
        this.shelfs = []
        this.pageNum = 1;
        this.first = true;
        this.last = false
        if (tab.props.name === '3') {
          this.getTimeline()
        } else if (tab.props.name === '2') {
          this.getBookShelf()
        }
      }
    },
    getTimeline() {
      const data = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }
      get(timeline, data).then(res => {
        const list = this.timeline
        this.timeline = list.concat(res.data.content)
        this.first = res.data.first
        this.last = res.data.last
        this.pageNum = this.pageNum + 1
      })
    },
    getBookShelf() {
      const loading = ElLoading.service({fullscreen: true})
      const data = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }
      get(shelfs, data).then(res => {
        const list = this.shelfs
        this.shelfs = list.concat(res.data.content)
        this.first = res.data.first
        this.last = res.data.last
        this.pageNum = this.pageNum + 1
        loading.close()
      }).catch(() => {
        loading.close()
      })
    },
    loadMore() {
      if (this.activeName === '2') {
        this.getBookShelf()
      } else if (this.activeName === '3') {
        this.getTimeline()
      }
    },
    submitForm() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          const data = {
            password: this.form.password,
            password1: this.form.password1,
            password2: this.form.password2,
          }
          put(repass, data).then(res => {
            if (res.code === 200) {
              this.$store.dispatch('Logout').then(() => {
                this.$router.push("/login")
              })
            }
          })
        } else {
          return false
        }
      })
    },
    toDetail(id) {
      this.$router.push("/detail/" + id)
    },
    toContent(id, bid, name, catalog) {
      this.$router.push("/content/" + id + "/" + bid + "/" + name + "/" + catalog)
    },
    delShelf(id, index) {
      const loading = ElLoading.service({fullscreen: true})
      del(delShelf + id).then(res => {
        if (res.code === 200) {
          this.shelfs = []
          this.pageNum = 1
          this.first = true
          this.last = false
          this.getBookShelf()
          loading.close()
        } else {
          loading.close()
          return ElMessage.error('删除失败，请重试.')
        }
      }).catch(() => {
        loading.close()
      })
    }
  }
}
</script>

<style scoped>
ul {
  padding-inline-start: 4px;
}

.b_title {
  display: flex;
  flex: 2;
  flex-direction: column;
  padding-left: 5px;
}

.b_desc {
  display: inline;
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 14px;
}

.grid-content {
  padding: 5px 2px;
  border: 1px solid #eeeeee;
  border-radius: 4px;
  align-items: center;
  display: flex;
  min-height: 100px;
}

.btn-div {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding-top: 5px;
  padding-right: 20px
}
</style>
